package com.xinbochuang.machine.admin.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.AlarmCount;
import com.xinbochuang.machine.admin.domain.CameraAlarmRecord;
import com.xinbochuang.machine.admin.mapper.CameraAlarmRecordMapper;
import com.xinbochuang.machine.admin.service.ICameraAlarmRecordService;
import com.xinbochuang.machine.common.utils.CustomDateUtils;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杜文博
 * @date 2020-10-29
 */
@Service
public class CameraAlarmRecordServiceImpl extends ServiceImpl<CameraAlarmRecordMapper, CameraAlarmRecord> implements ICameraAlarmRecordService {

    @Resource
    CameraAlarmRecordMapper cameraAlarmRecordMapper;

    @Resource
    TokenService tokenService;

    @Override
    public IPage<CameraAlarmRecord> selectList(CameraAlarmRecord cameraAlarmRecord) {
        QueryWrapper<CameraAlarmRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CameraAlarmRecord::getDeleteFlag, 0);
//        queryWrapper.lambda().orderByAsc(CameraAlarmRecord::getUpdateTime);
        queryWrapper.lambda().orderByDesc(CameraAlarmRecord::getRecordTime);
        if (cameraAlarmRecord != null) {
            if (StrUtil.isNotBlank(cameraAlarmRecord.getMacName())) {
                queryWrapper.lambda().like(CameraAlarmRecord::getMacName, cameraAlarmRecord.getMacName());
            }
            String state =cameraAlarmRecord.getCheckStatus()+"";
            if (!state.equals("null")) {
                queryWrapper.lambda().like(CameraAlarmRecord::getCheckStatus, cameraAlarmRecord.getCheckStatus());
            }
            if (StrUtil.isNotBlank(cameraAlarmRecord.getBeginTime()) && StrUtil.isNotBlank(cameraAlarmRecord.getEndTime())) {
                queryWrapper.lambda().between(CameraAlarmRecord::getRecordTime, cameraAlarmRecord.getBeginTime(), cameraAlarmRecord.getEndTime());
            }
        }
        queryWrapper.lambda().orderByDesc(CameraAlarmRecord::getRecordTime);
        return page(new Page<>(cameraAlarmRecord.getPageNum(), cameraAlarmRecord.getPageSize()), queryWrapper);
    }

    @Override
    public boolean updateCamera(CameraAlarmRecord cameraAlarmRecord) {
        CameraAlarmRecord result = cameraAlarmRecordMapper.selectById(cameraAlarmRecord.getUuid());
        result.setCheckStatus(cameraAlarmRecord.getCheckStatus());
        result.setRemark(cameraAlarmRecord.getRemark());
        result.setUpdateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getNickname());
        result.setUpdateTime(DateUtil.now());
        return updateById(result);
    }

    /**
     * 告警统计21
     *
     * @return 结果
     */
    @Override
    public List<AlarmCount> alarmCount() {
        return cameraAlarmRecordMapper.selectAlarmCount(CustomDateUtils.getThisWeekList());
    }
}
